import java.util.Random;
import java.util.Scanner;

public class Lab_01 {
    public static void main(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int correctCount = 0;

        System.out.println("===== 100以内加减法口算练习 =====");
        System.out.println("共50道题，请输入答案（整数）：");

        for (int i = 0; i < 50; i++) {
            // 生成两个100以内的随机数
            int num1 = random.nextInt(100);
            int num2 = random.nextInt(100);

            // 随机决定是加法还是减法（0为加法，1为减法）
            int operator = random.nextInt(2);
            int correctAnswer;
            String question;

            if (operator == 0) {
                // 加法运算
                correctAnswer = num1 + num2;
                question = String.format("第%d题：%d + %d = ?", i + 1, num1, num2);
            } else {
                // 减法运算（确保结果非负）
                if (num1 < num2) {
                    // 交换两个数，避免出现负数结果
                    int temp = num1;
                    num1 = num2;
                    num2 = temp;
                }
                correctAnswer = num1 - num2;
                question = String.format("第%d题：%d - %d = ?", i + 1, num1, num2);
            }

            // 输出题目并获取用户答案
            System.out.print(question + " ");
            int userAnswer;
            try {
                userAnswer = scanner.nextInt();
            } catch (Exception e) {
                // 处理输入非整数的情况，先判断是否有内容再清除
                System.out.println("输入错误，视为答错");
                if (scanner.hasNext()) {
                    scanner.next(); // 清除错误输入（仅当有内容时）
                }
                continue;
            }

            // 判断答案是否正确
            if (userAnswer == correctAnswer) {
                System.out.println("正确！");
                correctCount++;
            } else {
                System.out.println("错误，正确答案是：" + correctAnswer);
            }
        }

        // 输出最终成绩
        System.out.println("\n===== 练习结束 =====");
        System.out.println("总题数：50道");
        System.out.println("做对：" + correctCount + "道");
        System.out.println("做错：" + (50 - correctCount) + "道");
        System.out.println("正确率：" + (correctCount * 100.0 / 50) + "%");

        scanner.close();
    }
}